﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data.MySqlClient;

namespace wspedidosmovel
{
    public class CLSDB
    {

        /// <summary>
        /// Seta o alias do banco
        /// </summary>
        static private string _aliasBanco;
        static public string aliasBanco
        {
            get { return _aliasBanco; }
            set { _aliasBanco = value; }
        }


        static public MySqlConnection conexao = new MySqlConnection("server=localhost; userid=" + aliasBanco + ";pooling=yes;Database=PedidosMovel");
       
        static public void conectaDB()
        {
            if (conexao.State == System.Data.ConnectionState.Closed)
            {
                conexao.ConnectionString = "server=localhost; userid=" + aliasBanco + ";pooling=yes;Database=PedidosMovel";
                conexao.Open();
            }
        }

        static public void desconectaDB()
        {
            conexao.Close();
        }

        /// <summary>
        /// Executa uma query e retorna um DataReader
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        static public MySqlDataReader consulta(string query)
        {
            MySqlCommand comando = new MySqlCommand(query, conexao);
            MySqlDataReader reader = comando.ExecuteReader();
            return reader;
        }

        /// <summary>
        /// Retorna a primeira linha da primeira coluna
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        static public string consultaEscalar(string query)
        {
            MySqlCommand comando = new MySqlCommand(query, conexao);
            try
            {
                return (comando.ExecuteScalar()).ToString();
            }
            catch
            {
                return "0";
            }
        }
        static public void executar(string query)
        {
            MySqlCommand comando = new MySqlCommand(query, conexao);
            comando.ExecuteNonQuery();
        }
    }
}
